package com.zlj.jdbc.demo;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class SingleJdbcUtils {
	private Properties prop = null;
	private Connection conn = null;
	
	private SingleJdbcUtils() {
		init();
	}
	
	private void init() {
		InputStream stream = SingleJdbcUtils.class.getClassLoader().getResourceAsStream("dbConfig.properties");
		prop = new Properties();
		try {
			prop.load(stream);
			Class.forName(prop.getProperty("driverClassName"));
			conn = DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("username"), prop.getProperty("password"));
		} catch (IOException | ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	private static class LazyLoader {
		private static SingleJdbcUtils INSTANCE = new SingleJdbcUtils();
	}
	
	public static SingleJdbcUtils getInstance() {
		return LazyLoader.INSTANCE;
	}
	
	public Connection getConnection() {
		return conn;
	}
}
